
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../entity/reader_entity.dart';
import '../event/wrong_way_event.dart';
import '../main.dart';
import '../page/setting_page.dart';
import '../util/timer_manager.dart';
import '../util/toast_util.dart';
import 'circle_progress_widget.dart';

class DialogWidget{

  adminLoginDialog(){
    var psw = '000000';
    if(settingEntity.psw != null && settingEntity.psw != ''){
      psw = settingEntity.psw!;
    }
    var _psw = '';
    Get.defaultDialog(
      title: "",
      barrierDismissible: true,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        height: 230.h,
        width: 300.w,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/admin_login_bg.png",
              ),
            )
        ),
        alignment: Alignment.center,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
                decoration:BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(16.r)),
                  border: Border.all(
                    color: Colors.blue,
                    width: 0.3,
                  ),
                ),
                padding: EdgeInsets.only(left: 10.r,right: 10.r,top: 10.r,bottom: 10.r),
                margin:  EdgeInsets.only(left: 35.w, right: 35.w, bottom: 5.h,top: 90.h),
                child: TextField(
                    obscureText:true,
                    minLines: 1,
                    onChanged: (input){
                      _psw = input;
                    },
                    decoration: const InputDecoration(
                      isCollapsed: true,
                      border: InputBorder.none,
                      hintText: "请输入登录口令",
                    ),
                    style: TextStyle(color: Colors.black,fontSize: 8.sp)
                )
            ),
            InkWell(
              onTap: (){
                if( _psw.toString().isEmpty){
                  return;
                }
                if(_psw.toUpperCase()== psw){
                  Get.back();
                  Get.to(()=>const Setting());
                }else{
                  toastWarn('口令错误');
                }
              },
              child:Image.asset("assets/images/admin_login.png",width: 160.w, fit: BoxFit.fitWidth,),
            ),
          ],
        ),
      )
    );
  }


  doorOpenDialog(ReaderEntity readerEntity){
    tts.speak('操作完成后，请关闭柜门');
    timer.startCountDown(CountDownMode.slow,(){
      eventFactory.fire(WrongWayEvent());
      Get.until((route) => route.isFirst);
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Column(
        children: [
          Container(
              width: 240.w,
              height: 70.h,
              decoration: const BoxDecoration(
                  image: DecorationImage(
                    fit: BoxFit.fill,
                    image: AssetImage(
                      "assets/images/person_info.png",
                    ),
                  )
              ),
              child: Stack(
                alignment: Alignment.center,
                children: [
                  Positioned(
                    left: 30,
                    child: Image.asset("assets/images/head.png", width: 40.w, fit: BoxFit.fitWidth,),
                  ),
                  Positioned(
                    left: 60.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(readerEntity.name??'',style: TextStyle(color: Colors.blue,fontWeight: FontWeight.bold,fontSize: 12.sp),),
                        SizedBox(height: 3.h,),
                        Text('读者证号:${readerEntity.no}',style: TextStyle(color: Colors.blue,fontSize: 8.sp)),
                      ],
                    ),
                  ),
                  Positioned(
                      right: 30,
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          CircleProgress(size: 40.w,strokeWidth: 2.w,percent: readerEntity.currentBorrowCount!/readerEntity.limitCount!,color: Colors.blue,),
                          Container(
                            width: 34.w,
                            height: 34.w,
                            decoration: BoxDecoration(
                              color: Colors.blue,
                              borderRadius: BorderRadius.all(Radius.circular(25.r)),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text('${readerEntity.currentBorrowCount}/${readerEntity.limitCount}',style: TextStyle(color: Colors.white,fontSize: 8.sp,fontWeight: FontWeight.bold),),
                                Text('借阅数量',style: TextStyle(color: Colors.white,fontSize: 6.sp),)
                              ],
                            ),
                          )
                        ],
                      )
                  ),
                ],
              )
          ),
          SizedBox(height: 5.h,),
          Container(
            width: 240.w,
            height: 305.h,
            decoration: const BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/dialog_bg.png",
                  ),
                )
            ),
            child: Stack(
              alignment: Alignment.topCenter,
              children: [
                Positioned(
                    top: 5.h,
                    child: Container(
                      height: 30.h,
                      alignment: Alignment.center,
                      child: Text('柜门已打开',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 18.sp,)),
                    )
                ),
                Positioned(
                    top: 5.h,
                    right: 10.w,
                    child: Container(
                        height: 30.h,
                        alignment: Alignment.center,
                        child: Obx(() => Text('${timer.slowTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,),)
                    )
                ),
                Positioned(
                    top: 105.h,
                    left: 10.w,
                    right: 10.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Image.asset("assets/images/open_guide.png",height: 90.h, fit: BoxFit.fitHeight,),
                        SizedBox(height: 25.h,),
                        Text('请把书籍朝上放置在指定区域内\n请关闭柜门结算',style: TextStyle(fontSize: 12.sp, color: Colors.red, fontWeight: FontWeight.bold),textAlign: TextAlign.center,)
                      ],
                    )
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  adminDoorOpenDialog(){
    tts.speak('操作完成后，请关闭柜门');
    timer.startCountDown(CountDownMode.slow,(){
      eventFactory.fire(WrongWayEvent());
      Get.until((route) => route.isFirst);
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 240.w,
        height: 305.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/dialog_bg.png",
              ),
            )
        ),
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Positioned(
                top: 5.h,
                child: Container(
                  height: 30.h,
                  alignment: Alignment.center,
                  child: Text('柜门已打开',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 18.sp,)),
                )
            ),
            Positioned(
                top: 5.h,
                right: 10.w,
                child: Container(
                    height: 30.h,
                    alignment: Alignment.center,
                    child: Obx(() => Text('${timer.slowTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,),)
                )
            ),
            Positioned(
                top: 105.h,
                left: 10.w,
                right: 10.w,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Image.asset("assets/images/open_guide.png",height: 90.h, fit: BoxFit.fitHeight,),
                    SizedBox(height: 25.h,),
                    Text('请关闭柜门结算',style: TextStyle(fontSize: 12.sp, color: Colors.red, fontWeight: FontWeight.bold),textAlign: TextAlign.center,)
                  ],
                )
            ),
          ],
        ),
      ),
    );
  }

  warnOpenDialog(){
    tts.speak('请先关闭柜门');
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 300.w,
        height: 208.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/warn.png",
              ),
            )
        ),
      ),
    );
  }

  borrowOverDialog({bool showOver = false}){
    if(showOver){
      tts.speak('借书已超限，请及时归还');
    }else{
      tts.speak('操作成功');
    }
    timer.startCountDown(CountDownMode.five,(){
      Get.until((route) => route.isFirst);
    });
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
          width: 200.w,
          height: 185.h,
          decoration: const BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage(
                  "assets/images/borrow_result_bg.jpg",
                ),
              )
          ),
          child: Stack(
            children: [
              Visibility(
                visible: showOver,
                child: Positioned(
                    top: 151.h,
                    left: 0.w,
                    right: 0.w,
                    child: InkWell(
                        onTap: (){
                          timer.cancel();
                          Get.until((route) => route.isFirst);
                        },
                        child: Container(width:200.w,alignment:Alignment.center,
                          child: Text('借书量已超限，请及时归还',style: TextStyle(color: Colors.red,fontSize: 10.sp,fontWeight: FontWeight.bold),),)
                    )
                ),),
              Positioned(
                  right: 28.w,
                  top: 2.h,
                  child: Container(
                    height: 30.h,
                    alignment: Alignment.center,
                    child: Obx(() => Text('${timer.fiveTime.value}s',style: TextStyle(color: Colors.white,fontSize: 10.sp),textAlign: TextAlign.center,)),
                  )
              ),
              Positioned(
                  right: 10.w,
                  child: InkWell(
                    onTap: (){
                      timer.cancel();
                      Get.until((route) => route.isFirst);
                    },
                    child: Container(
                        height: 31.h,
                        alignment: Alignment.center,
                        child: Icon(Icons.close,color: Colors.white,size: 20.sp,)
                    ),
                  )
              ),
            ],
          )
      ),
    );
  }
}